package projet.dao.jpa;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;

import projet.dao.LocationDAOInterface;
import projet.entity.Location;
import projet.entity.Session;

public class JPALocationDAO implements LocationDAOInterface {

	@Override
	public List<Location> getList() {
		List<Location> locations = null;

		EntityManager em = JPAUtil.getEntityManagerFactory().createEntityManager();
		try {
			em.getTransaction().begin();

			TypedQuery<Location> query = em.createNamedQuery("Location.findAll", Location.class);
			locations = query.getResultList();

			em.getTransaction().commit();
		} catch (Exception e) {
			e.printStackTrace();
			try { em.getTransaction().rollback(); } catch (Exception e2) { e2.printStackTrace(); }
		} finally {
			try { em.close(); } catch (Exception e2) { e2.printStackTrace(); }
		}

		return locations;
	}

	@Override
	public Location getById(Long id) {
		Location location = null;

		EntityManager em = JPAUtil.getEntityManagerFactory().createEntityManager();
		try {
			em.getTransaction().begin();

			location = em.find(Location.class, id);

			em.getTransaction().commit();
		} catch (Exception e) {
			e.printStackTrace();
			try { em.getTransaction().rollback(); } catch (Exception e2) { e2.printStackTrace(); }
		} finally {
			try { em.close(); } catch (Exception e2) { e2.printStackTrace(); }
		}

		return location;
	}

	@Override
	public void create(Location location) {
		EntityManager em = JPAUtil.getEntityManagerFactory().createEntityManager();
		try {
			em.getTransaction().begin();

			em.persist(location);

			em.getTransaction().commit();
		} catch (Exception e) {
			e.printStackTrace();
			try { em.getTransaction().rollback(); } catch (Exception e2) { e2.printStackTrace(); }
		} finally {
			try { em.close(); } catch (Exception e2) { e2.printStackTrace(); }
		}
	}

	@Override
	public void update(Location location) {
		EntityManager em = JPAUtil.getEntityManagerFactory().createEntityManager();
		try {
			em.getTransaction().begin();

			em.merge(location);

			em.getTransaction().commit();
		} catch (Exception e) {
			e.printStackTrace();
			try { em.getTransaction().rollback(); } catch (Exception e2) { e2.printStackTrace(); }
		} finally {
			try { em.close(); } catch (Exception e2) { e2.printStackTrace(); }
		}
	}

	@Override
	public void delete(Location location) {
		EntityManager em = JPAUtil.getEntityManagerFactory().createEntityManager();
		try {
			em.getTransaction().begin();

			Location l = em.find(Location.class, location.getId());
			em.remove(l);

			em.getTransaction().commit();
		} catch (Exception e) {
			e.printStackTrace();
			try { em.getTransaction().rollback(); } catch (Exception e2) { e2.printStackTrace(); }
		} finally {
			try { em.close(); } catch (Exception e2) { e2.printStackTrace(); }
		}
	}

	@Override
	public List<Session> getSessions(Long id) {
		List<Session> sessions = null;

		EntityManager em = JPAUtil.getEntityManagerFactory().createEntityManager();
		try {
			em.getTransaction().begin();

			TypedQuery<Session> query = em.createNamedQuery("Location.findSessions", Session.class);
			query.setParameter("id", id);
			sessions = query.getResultList();

			em.getTransaction().commit();
		} catch (Exception e) {
			e.printStackTrace();
			try { em.getTransaction().rollback(); } catch (Exception e2) { e2.printStackTrace(); }
		} finally {
			try { em.close(); } catch (Exception e2) { e2.printStackTrace(); }
		}

		return sessions;
	}

}
